<?php

class IndexController extends Zend_Controller_Action
{
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        $this->_auth        = new My_Authuser();
        $this->view->auth   = $this->_auth;
        $this->_auth        = $this->_auth->checkAuthentication();
        $this->view->auth   = $this->_auth;
    }
    
    public function indexAction(){
        $modelP = new Default_Model_Product();
        $dataPro=$modelP->getDataById($this->_params['proId']);
        if(isset($this->_params['act']) && $this->_params['act'] == 'form' && isset($this->_params['proId'])){
            $ip=$_SERVER['REMOTE_ADDR'];
            $modelView=new Default_Model_View();
            $dataIp=$modelView->getData(array('ip'=>$ip,'proId'=>$this->_params['proId']));
            if(!isset($dataIp) && !$dataIp){//neu chua ton tai thi +view
                $viewId=$modelView->addData(array('proId'=>$this->_params['proId'],'ip'=>$ip,'time'=>time()));
                $view=$dataPro['view']+1;
                $modelP->updateWait($this->_params['proId'],array('view'=>$view));
            }else{
            //neu time hien tai > time save+1, and +view
                if($dataIp['time']+86400 < time()){
                    $view=$dataPro['view']+1;
                    $modelView->updateData($dataIp['id'],array('time'=>time()));
                    $modelP->updateWait($this->_params['proId'],array('view'=>$view)); 
                }
            }
        }
        
        $navi='Tất cả chuyên mục';
        $checkLoad=1;
        $form = new Default_Form_Search();
        $modelItem = new Default_Model_Productitem();
        $modelCat= new Default_Model_Productcat();
        $modelArea = new Default_Model_Area();
        $paramsForm=array();
        $params=array();
        $paramsView=$this->_params;
        
        if(isset($this->_params['catId']) && $this->_params['catId']!=0 && $this->_params['catId']!='ca'){
            $params['catId']=$this->_params['catId'];
            $paramsForm['itemId']='cat_'.$this->_params['catId'];
        }else $paramsView['catId']='ca';
        //pr($this->_params);
        if(isset($this->_params['itemId']) && $this->_params['itemId']!=-1 && !is_numeric($this->_params['itemId']) && $this->_params['itemId']!='it'){
            $paramsForm['itemId']=$this->_params['itemId'];
            $cat = explode("_", $this->_params['itemId']);
            $params['catId']=$cat[1]; 
        }
        elseif(isset($this->_params['itemId']) && $this->_params['itemId']!=-1 && is_numeric($this->_params['itemId']) && $this->_params['itemId']!='it'){
            $paramsForm['itemId']=$this->_params['itemId'];
            $params['itemId']=$this->_params['itemId'];
        }else $paramsView['itemId']='it';
        if(isset($this->_params['title']) && $this->_params['title'] && $this->_params['title']!='eltit'){
            $paramsForm['title']=$this->_params['title'];
            $params['title']=$this->_params['title'];
        }else $paramsView['title'] = 'eltit';
        if(isset($this->_params['areaId']) && $this->_params['areaId']!=-1 && $this->_params['areaId']!=0){
            $paramsForm['areaId']=$this->_params['areaId'];
            $params['areaId']=$this->_params['areaId'];
        }else $paramsView['areaId'] = 0;
        if(isset($this->_params['transaction']) && $this->_params['transaction'] && $this->_params['transaction']!='gd'){
            $paramsForm['transaction']=$this->_params['transaction'];
            $params['transaction']=$this->_params['transaction'];
        }else $paramsView['transaction']='gd';
        if($params!=null){
            $checkLoad=0;
        }
        
        if($this->_params['view']==1){
            $paramsForm['view']=$this->_params['view'];
            $params['view'] = $this->_params['view'];
        }else $paramsView['view'] = 0;
        $header='Rao Vặt';
        if(isset($this->_params['areaId']) && $this->_params['areaId']!=-1 && $this->_params['areaId']!=0){            
            $area = $modelArea->getDataById($this->_params['areaId'])->toArray();
            if(isset($this->_params['itemId']) && $this->_params['itemId']!=-1 && $this->_params['itemId']!='it'){
                if(is_numeric($this->_params['itemId'])){
                    $item = $modelItem->getDataById($this->_params['itemId'])->toArray();
                    $navi = $area['name'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['name'];
                }else{                    
                    $cat = explode("_", $this->_params['itemId']);
                    $item = $modelCat->getDataById($cat[1])->toArray();
                    $navi = $area['name'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['name'];                    
                }                                                
            }else{
                $navi = $area['name'];
            }                
        }elseif(isset($this->_params['itemId']) && $this->_params['itemId']!=-1 && $this->_params['itemId']!='it'){            
            if(is_numeric($this->_params['itemId'])){
                $item = $modelItem->getDataById($this->_params['itemId'])->toArray();
                $navi = $item['catName'].'&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;'.$item['name'];
				$header=$item['name'];
            }else{
                $cat = explode("_", $this->_params['itemId']);
                $item = $modelCat->getDataById($cat[1])->toArray();
                $navi = $item['name'];
				$header=$item['name'];
            }
        }
        elseif(isset($this->_params['catId']) && $this->_params['catId']!=0){            
            $dataCat=$modelCat->getDataById($this->_params['catId']);
            $navi = $dataCat['name'];
			$header=$dataCat['name'];
        }
        $this->view->form = $form;    
        $this->view->header=$header;      
        $form->populate($paramsForm);   

        $params['wait']=2;
        $total = $modelP->countData($params); 
        $this->view->total=$total;
        //get all data
        $page                           = $this->_getParam('page', 1);
        $itemPerPage                    = 20;
        $pageRange                      = 10;
        $offset                         = 0;
        if($page > 0){
            if($total > 0){
                $paginator              = new My_Paginator();
                if(isset($config['pagination']['itemPerPage']))
                    $itemPerPage        = $config['pagination']['itemPerPage'];
                if(isset($config['pagination']['pageRange']))
                    $pageRange          = $config['pagination']['pageRange'];

                $offset                 = ($page-1)*$itemPerPage;
                $createPaginator        = $paginator->createPaginator($total, $itemPerPage, $pageRange, $page);
                $this->view->paginator  = $createPaginator;
            }
        }
        $limit               = "$itemPerPage,$offset";
         
        $params['limit']=$limit;
        $dataPro=$modelP->getAllData($params); 
        $params['human']='pe';
        $totalPerson = $modelP->countData($params); 
        $this->view->totalPerson=$totalPerson;
        if($page > 0){
            if($totalPerson > 0){
                $paginatorPe              = new My_Paginator();
                if(isset($config['pagination']['itemPerPage']))
                    $itemPerPage        = $config['pagination']['itemPerPage'];
                if(isset($config['pagination']['pageRange']))
                    $pageRange          = $config['pagination']['pageRange'];

                $offset                 = ($page-1)*$itemPerPage;
                $createPaginator        = $paginatorPe->createPaginator($totalPerson, $itemPerPage, $pageRange, $page);
                $this->view->paginatorPe  = $createPaginator;
            }
        }
        $dataPerson =$modelP->getAllData($params);
        
        $params['human']='co';
        $totalCompany = $modelP->countData($params); 
        $this->view->totalCompany=$totalCompany;
        if($page > 0){
            if($totalCompany > 0){
                $paginatorCo              = new My_Paginator();
                if(isset($config['pagination']['itemPerPage']))
                    $itemPerPage        = $config['pagination']['itemPerPage'];
                if(isset($config['pagination']['pageRange']))
                    $pageRange          = $config['pagination']['pageRange'];

                $offset                 = ($page-1)*$itemPerPage;
                $createPaginator        = $paginatorCo->createPaginator($totalCompany, $itemPerPage, $pageRange, $page);
                $this->view->paginatorCo  = $createPaginator;
            }
        }        
        $dataCompany = $modelP->getAllData($params);
           
        $proNewest = $modelP->getDataNewest(10);
                
        $this->view->proNewest=$proNewest->toArray();
        $this->view->params=$paramsView;
        $this->view->checkLoad=$checkLoad;
        $this->view->proAll=$dataPro->toArray();
        $this->view->proCompany=$dataCompany->toArray();
        $this->view->proPerson=$dataPerson->toArray();
        $this->view->navi = $navi;
    }
        
}